package com.hx.haowumall.haowumallorder.controller;

import com.hx.haowumall.haowumallorder.entity.OrderEntity;
import com.hx.haowumall.haowumallorder.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public String sendMq(@RequestParam(value = "num",defaultValue = "10") Integer num) {
        for (int i=0;i<num;i++){

            if (i%2 == 0){
                //1、发送消息，发送的消息是个对象，我们会使用序列化机制，将对象写出去，对象必须实现Serializable
                OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
                reasonEntity.setId(1L);
                reasonEntity.setCreateTime(new Date());
                reasonEntity.setName("阿萨*"+i);

                //2、发送消息的对象类型可以是一个JSON
                rabbitTemplate.convertAndSend("hello.java.exchange","hello.java",reasonEntity,
                        new CorrelationData(UUID.randomUUID().toString()));
            }else {
                //1、发送消息，发送的消息是个对象，我们会使用序列化机制，将对象写出去，对象必须实现Serializable
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderSn(UUID.randomUUID().toString());
                //2、发送消息的对象类型可以是一个JSON
                rabbitTemplate.convertAndSend("hello.java.exchange","hello2.java",orderEntity,
                        new CorrelationData(UUID.randomUUID().toString()));

            }

        }
        return "ok";
    }
}
